﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PMSA.Framework.Log;

public partial class Admin_Uc_Product_EditGroupProduct : System.Web.UI.UserControl
{
    private int groupProducId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["gpi"] != null)
        {
            try
            {
                groupProducId = int.Parse(Request.QueryString["gpi"].ToString());
            }
            catch
            {
                CLogManager.Write("GroupProduct.Edit.PageLoad", "groupProducId not a number: " + Request.QueryString["gpi"].ToString(), "ADMIN");
            }
        }
        if (IsPostBack)
        {

        }
        else
        {
            CreateDDLGroupProductParent();
            GetDetailGroupProduct(groupProducId);
        }
    }

    private void GetDetailGroupProduct(int groupProducId)
    {
        try
        {
            GroupProduct obj = new GroupProduct();
            obj = obj.Detail(groupProducId);
            txtGroupProductName.Text = obj.GroupProductName;
            txtStatus.Text = obj.Status.ToString();
            ddlGroupProductParent.SelectedValue = obj.GroupParent.ToString();
            txtGroupProductActive.Checked = obj.IsActive == 1 ? true : false;
        }
        catch (Exception ex)
        {
            CLogManager.Write("GroupProduct.Edit.GetDetailGroupProduct", ex.ToString(), "ADMIN");
        }
    }
    private void CreateDDLGroupProductParent()
    {
        try
        {
            GroupProductLevelParent obj = new GroupProductLevelParent();
            IList<GroupProductLevelParent> list = obj.List(0);
            ddlGroupProductParent.DataValueField = "Id";
            ddlGroupProductParent.DataTextField = "Name";
            ddlGroupProductParent.DataSource = list;
            ddlGroupProductParent.DataBind();
        }
        catch (Exception ex)
        {
            CLogManager.Write("GroupProduct.Edit.CreateDDLGroupProductParent", ex.ToString(), "ERR");
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            string name = txtGroupProductName.Text;
            int active = txtGroupProductActive.Checked ? 1 : 0;
            int groupParent = int.Parse(ddlGroupProductParent.SelectedValue);
            int status = int.Parse(txtStatus.Text);

            if ((new GroupProduct().Update(groupProducId, name, active, groupParent, status)) != null)
            {
                Response.Write("<script type='text/javascript'>alert('Edit Successful !');</script>");
            }
        }
        catch (Exception ex)
        {
            CLogManager.Write("GroupProduct.Edit.btnEdit_Click", ex.ToString(), "ADMIN");
        }
    }
}